package com.gm.controller.role;

import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.gm.common.Pager;
import com.gm.model.po.Right;
import com.gm.model.po.Role;
import com.gm.service.user.RightService;
import com.gm.service.user.RoleSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * Created by yangxing on 2017/6/10.
 */
@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleSerivce roleSerivce;
    @Autowired
    private RightService rightService;


    @RequestMapping("roleList.web")
    public ModelAndView roleList(Pager pager){
        ModelAndView view= new ModelAndView();
        List roleList = roleSerivce.getALlRoleList(pager);
        PageList pageList = (PageList)roleList;
        Paginator paginator = pageList.getPaginator();
        view.addObject("roleList", pageList);
        view.addObject("paginator", paginator);
        view.setViewName("../role/role.jsp");
        return view;
    }


    @RequestMapping("gotoAddRole.web")
    public ModelAndView gotoAddRole(){
        ModelAndView view= new ModelAndView();
        List<Right> rightList = rightService.getALlRightList();
        view.addObject("rightList", rightList);
        view.setViewName("../role/addRole.jsp");
        return view;
    }

    @RequestMapping("gotoUpdateRole.web")
    public ModelAndView gotoUpdateRole(Integer roleId,Integer currentPage){
        ModelAndView view= new ModelAndView();
        Role role = roleSerivce.selectByPrimaryKey(roleId);
        List<Right> rightList = rightService.getALlRightList();
        view.addObject("rightList", rightList);
        view.addObject("currentPage", currentPage);
        view.addObject("role", role);
        view.addObject("rights", role.getRights());
        view.setViewName("../role/updateRole.jsp");
        return view;
    }


    @RequestMapping("updateRole.web")
    public ModelAndView updateRole(Role role,Integer[] rightId,Integer currentPage,RedirectAttributes attr){
        ModelAndView view= new ModelAndView();
        int ret = roleSerivce.updateRole(role,rightId);
        view.addObject("currentPage", currentPage);
        attr.addFlashAttribute("msg","修改成功");
        view.setViewName("redirect:../role/roleList.web");
        return view;
    }


    @RequestMapping("addRole.web")
    public ModelAndView addRole(String roleName,Integer[] rightId,RedirectAttributes attr){
        ModelAndView view= new ModelAndView();
        int ret = roleSerivce.addRole(roleName,rightId);
        if(ret==1){
            attr.addFlashAttribute("msg", "添加成功");
        }else{
            attr.addFlashAttribute("msg", "添加失败");
        }
        view.setViewName("redirect:../role/roleList.web");
        return view;
    }

    @RequestMapping("deleteRole.web")
    public ModelAndView deleteRole(Integer roleId,Integer currentPage,RedirectAttributes attr){
        ModelAndView view= new ModelAndView();
        int ret = roleSerivce.deleteRole(roleId);
        if(ret==1){
            attr.addFlashAttribute("msg", "删除成功");
        }else{
            attr.addFlashAttribute("msg", "删除失败");
        }
        Pager pager = new Pager();
        pager.setCurrentPage(currentPage);
        List roleList = roleSerivce.getALlRoleList(pager);
        if(roleList.size() == 0){
            currentPage = currentPage -1;
        }
        if(currentPage==0)currentPage=1;
        view.addObject("currentPage", currentPage);
        view.setViewName("redirect:../role/roleList.web");
        return view;
    }

}
